﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using SD.DataAccess;
using SD.Business;

namespace SD.Web.Views.Government
{
    public partial class EditGovOfficeRegion : System.Web.UI.Page
    {
        /// <summary>
        /// Page load event
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">event arg</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGovOfficeRegion();
            }
        }

        /// <summary>
        /// Loads the gov office region.
        /// </summary>
        private void LoadGovOfficeRegion()
        {
            int id = 0;
            if (int.TryParse(Request.Params[0], out id))
            {
                GovOfficeRegion govOfficeRegion = GovOfficeRegionBus.Get(id);
                if (govOfficeRegion != null)
                {
                    GovOfficeRegionFormView.DataSource = new[] { govOfficeRegion };
                    GovOfficeRegionFormView.DataBind();

                    var btnSetActive = GovOfficeRegionFormView.FindControl("ButtonSetActive") as Button;

                    if (btnSetActive != null)
                    {
                        if (govOfficeRegion.IsActive)
                        {
                            btnSetActive.Text = "Deactive";
                        }
                        else
                        {
                            btnSetActive.Text = "Active";
                        }
                    }

                    var textBoxCounty = GovOfficeRegionFormView.FindControl("TextBoxCounty") as TextBox;
                    if (textBoxCounty != null)
                    {
                        textBoxCounty.Text = string.Join("\r\n", govOfficeRegion.Counties.Select(c => c.CountyName));
                    }
                    return;
                }
            }

            GovOfficeRegionFormView.DataSource = new object[0];
            GovOfficeRegionFormView.DataBind();
        }

        /// <summary>
        /// Back button click event
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">event arg</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Views/Government/GovOfficeRegionList.aspx");
        }

        /// <summary>
        /// Save button click event
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">event arg</param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            int id = 0;
            if (int.TryParse(Request.Params[0], out id))
            {
                var lblMessage = GovOfficeRegionFormView.FindControl("LabelMessage") as Label;
                var txtName = GovOfficeRegionFormView.FindControl("TextBoxName") as TextBox;
                var txtDescription = GovOfficeRegionFormView.FindControl("TextBoxDescription") as TextBox;

                lblMessage.ForeColor = Color.Red;
                string name = txtName.Text;

                try
                {
                    GovOfficeRegion gor = new GovOfficeRegion();
                    gor.GovOfficeRegionId = id;
                    gor.GovOfficeRegionName = name;
                    gor.Description = txtDescription.Text.Trim();
                    if (!GovOfficeRegionBus.Update(gor))
                    {
                        throw new Exception("The system is not responding now, please try again later!");
                    }
                    lblMessage.ForeColor = Color.BlueViolet;
                    lblMessage.Text = "Saving successfully!";
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
                return;
            }

            GovOfficeRegionFormView.DataSource = new object[0];
            GovOfficeRegionFormView.DataBind();
        }

        /// <summary>
        /// SetActive button click event
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">event arg</param>
        protected void ButtonSetActive_Click(object sender, EventArgs e)
        {
            int id = 0;
            if (int.TryParse(Request.Params[0], out id))
            {
                GovOfficeRegion govOfficeRegion = GovOfficeRegionBus.Get(id);
                if (govOfficeRegion != null)
                {
                    bool status = !govOfficeRegion.IsActive;

                    var btnSetActive = GovOfficeRegionFormView.FindControl("ButtonSetActive") as Button;
                    if (btnSetActive != null)
                    {
                        if (btnSetActive.Text == "Active")
                        {
                            status = true;
                        }
                        else
                        {
                            status = false;
                        }
                    }

                    GovOfficeRegionBus.SetActiveStatus(id, status);
                    LoadGovOfficeRegion();
                }
            }
            else
            {
                GovOfficeRegionFormView.DataSource = new object[0];
                GovOfficeRegionFormView.DataBind();
            }
        }
    }
}